home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gschar0.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.3 KB  |  414 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gschar0.c,v 1.2.2.1 2000/10/26 12:45:11 igorm Exp $ */
  20. /* Composite font decoding for Ghostscript library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gsfcmap.h"
  26. #include "gxfixed.h"
  27. #include "gxdevice.h"
  28. #include "gxfont.h"
  29. #include "gxfont0.h"
  30. #include "gxtext.h"
  31.  
  32. /* Stack up modal composite fonts, down to a non-modal or base font. */
  33. private int
  34. gs_stack_modal_fonts(gs_text_enum_t *pte)
  35. {
  36.     int fdepth = pte->fstack.depth;
  37.     gs_font *cfont = pte->fstack.items[fdepth].font;
  38.  
  39.     while (cfont->FontType == ft_composite) {
  40.     gs_font_type0 *const cmfont = (gs_font_type0 *) cfont;
  41.  
  42.     if (!fmap_type_is_modal(cmfont->data.FMapType))
  43.         break;
  44.     if (fdepth == MAX_FONT_STACK)
  45.         return_error(gs_error_invalidfont);
  46.     fdepth++;
  47.     cfont = cmfont->data.FDepVector[cmfont->data.Encoding[0]];
  48.     pte->fstack.items[fdepth].font = cfont;
  49.     pte->fstack.items[fdepth].index = 0;
  50.     if_debug2('j', "[j]stacking depth=%d font=0x%lx\n",
  51.           fdepth, (ulong) cfont);
  52.     }
  53.     pte->fstack.depth = fdepth;
  54.     return 0;
  55. }
  56. /* Initialize the composite font stack for a show enumerator. */
  57. /* Return an error if the data is not a byte string. */
  58. int
  59. gs_type0_init_fstack(gs_text_enum_t *pte, gs_font * pfont)
  60. {
  61.     if (!(pte->text.operation & (TEXT_FROM_STRING | TEXT_FROM_BYTES)))
  62.     return_error(gs_error_invalidfont);
  63.     if_debug1('j', "[j]stacking depth=0 font=0x%lx\n",
  64.           (ulong) pfont);
  65.     pte->fstack.depth = 0;
  66.     pte->fstack.items[0].font = pfont;
  67.     pte->fstack.items[0].index = 0;
  68.     return gs_stack_modal_fonts(pte);
  69. }
  70.  
  71. /* Select the appropriate descendant of a font. */
  72. /* Uses free variables: pte. */
  73. /* Uses pdata, uses & updates fdepth, sets pfont. */
  74. #define select_descendant(pfont, pdata, fidx, fdepth)\
  75.   if (fidx >= pdata->encoding_size)\
  76.     return_error(gs_error_rangecheck);\
  77.   if (fdepth == MAX_FONT_STACK)\
  78.     return_error(gs_error_invalidfont);\
  79.   pfont = pdata->FDepVector[pdata->Encoding[fidx]];\
  80.   if (++fdepth > orig_depth || pfont != pte->fstack.items[fdepth].font)\
  81.     pte->fstack.items[fdepth].font = pfont, changed = 1;\
  82.   pte->fstack.items[fdepth].index = fidx
  83.  
  84. /* Get the root EscChar of a composite font, which overrides the EscChar */
  85. /* of descendant fonts. */
  86. private uint
  87. root_esc_char(const gs_text_enum_t *pte)
  88. {
  89.     return ((gs_font_type0 *) (pte->fstack.items[0].font))->data.EscChar;
  90. }
  91.  
  92. /* Get the next character or glyph from a composite string. */
  93. /* If we run off the end of the string in the middle of a */
  94. /* multi-byte sequence, return gs_error_rangecheck. */
  95. /* If the string is empty, return 2. */
  96. /* If the current (base) font changed, return 1.  Otherwise, return 0. */
  97. int
  98. gs_type0_next_char_glyph(gs_text_enum_t *pte, gs_char *pchr, gs_glyph *pglyph)
  99. {
  100.     const byte *str = pte->text.data.bytes;
  101.     const byte *p = str + pte->index;
  102.     const byte *end = str + pte->text.size;
  103.     int fdepth = pte->fstack.depth;
  104.     int orig_depth = fdepth;
  105.     gs_font *pfont;
  106.  
  107. #define pfont0 ((gs_font_type0 *)pfont)
  108.     gs_type0_data *pdata;
  109.     uint fidx;
  110.     gs_char chr;
  111.     gs_glyph glyph = gs_no_glyph;
  112.     int changed = 0;
  113.  
  114.     pte->FontBBox_as_Metrics2.x = pte->FontBBox_as_Metrics2.y = 0;
  115.  
  116. #define need_left(n)\
  117.   if ( end - p < n ) return_error(gs_error_rangecheck)
  118.  
  119.     /*
  120.      * Although the Adobe documentation doesn't say anything about this,
  121.      * if the root font is modal and the very first character of the
  122.      * string being decoded is an escape or shift character, then
  123.      * font selection via the escape mechanism works down from the root,
  124.      * rather than up from the lowest modal font.  (This was first
  125.      * reported by Norio Katayama, and confirmed by someone at Adobe.)
  126.      */
  127.  
  128.     if (pte->index == 0) {
  129.     int idepth = 0;
  130.  
  131.     pfont = pte->fstack.items[0].font;
  132.     for (; pfont->FontType == ft_composite;) {
  133.         fmap_type fmt = (pdata = &pfont0->data)->FMapType;
  134.  
  135.         if (p == end)
  136.         return 2;
  137.         chr = *p;
  138.         switch (fmt) {
  139.         case fmap_escape:
  140.             if (chr != root_esc_char(pte))
  141.             break;
  142.             need_left(2);
  143.             fidx = p[1];
  144.             p += 2;
  145.             if_debug1('j', "[j]from root: escape %d\n", fidx);
  146.           rdown:select_descendant(pfont, pdata, fidx, idepth);
  147.             if_debug2('j', "[j]... new depth=%d, new font=0x%lx\n",
  148.                   idepth, (ulong) pfont);
  149.             continue;
  150.         case fmap_double_escape:
  151.             if (chr != root_esc_char(pte))
  152.             break;
  153.             need_left(2);
  154.             fidx = p[1];
  155.             p += 2;
  156.             if (fidx == chr) {
  157.             need_left(1);
  158.             fidx = *p++ + 256;
  159.             }
  160.             if_debug1('j', "[j]from root: double escape %d\n", fidx);
  161.             goto rdown;
  162.         case fmap_shift:
  163.             if (chr == pdata->ShiftIn)
  164.             fidx = 0;
  165.             else if (chr == pdata->ShiftOut)
  166.             fidx = 1;
  167.             else
  168.             break;
  169.             p++;
  170.             if_debug1('j', "[j]from root: shift %d\n", fidx);
  171.             goto rdown;
  172.         default:
  173.             break;
  174.         }
  175.         break;
  176.     }
  177.     /* If we saw any initial escapes or shifts, */
  178.     /* compute a new initial base font. */
  179.     if (idepth != 0) {
  180.         int code;
  181.  
  182.         pte->fstack.depth = idepth;
  183.         code = gs_stack_modal_fonts(pte);
  184.         if (code < 0)
  185.         return code;
  186.         if (pte->fstack.depth > idepth)
  187.         changed = 1;
  188.         orig_depth = fdepth = pte->fstack.depth;
  189.     }
  190.     }
  191.     /* Handle initial escapes or shifts. */
  192.  
  193.   up:if (p == end)
  194.     return 2;
  195.     chr = *p;
  196.     while (fdepth > 0) {
  197.     pfont = pte->fstack.items[fdepth - 1].font;
  198.     pdata = &pfont0->data;
  199.     switch (pdata->FMapType) {
  200.         default:        /* non-modal */
  201.         fdepth--;
  202.         continue;
  203.  
  204.         case fmap_escape:
  205.         if (chr != root_esc_char(pte))
  206.             break;
  207.         need_left(2);
  208.         fidx = *++p;
  209.         if_debug1('j', "[j]next: escape %d\n", fidx);
  210.         /* Per Adobe, if we get an escape at the root, */
  211.         /* treat it as an ordinary character (font index). */
  212.         if (fidx == chr && fdepth > 1) {
  213.             fdepth--;
  214.             goto up;
  215.         }
  216.           down:if (++p == end)
  217.             return 2;
  218.         chr = *p;
  219.         fdepth--;
  220.         do {
  221.             select_descendant(pfont, pdata, fidx, fdepth);
  222.             if_debug3('j', "[j]down from modal: new depth=%d, index=%d, new font=0x%lx\n",
  223.                   fdepth, fidx, (ulong) pfont);
  224.             if (pfont->FontType != ft_composite)
  225.             break;
  226.             pdata = &pfont0->data;
  227.             fidx = 0;
  228.         }
  229.         while (pdata->FMapType == fmap_escape);
  230.         continue;
  231.  
  232.         case fmap_double_escape:
  233.         if (chr != root_esc_char(pte))
  234.             break;
  235.         need_left(2);
  236.         fidx = *++p;
  237.         if (fidx == chr) {
  238.             need_left(2);
  239.             fidx = *++p + 256;
  240.         }
  241.         if_debug1('j', "[j]next: double escape %d\n", fidx);
  242.         goto down;
  243.  
  244.         case fmap_shift:
  245.         if (chr == pdata->ShiftIn)
  246.             fidx = 0;
  247.         else if (chr == pdata->ShiftOut)
  248.             fidx = 1;
  249.         else
  250.             break;
  251.         if_debug1('j', "[j]next: shift %d\n", fidx);
  252.         goto down;
  253.     }
  254.     break;
  255.     }
  256.     /* At this point, chr == *p. */
  257.     /* (This is important to know for CMap'ed fonts.) */
  258.     p++;
  259.  
  260.     /*
  261.      * Now handle non-modal descendants.
  262.      * The PostScript language manual has some confusing
  263.      * wording about the parent supplying the "first part"
  264.      * of the child's decoding information; what this means
  265.      * is not (as one might imagine) the font index, but
  266.      * simply the first byte of the data.
  267.      */
  268.  
  269.     while ((pfont = pte->fstack.items[fdepth].font)->FontType == ft_composite) {
  270.     pdata = &pfont0->data;
  271.     switch (pdata->FMapType) {
  272.         default:        /* can't happen */
  273.         return_error(gs_error_invalidfont);
  274.  
  275.         case fmap_8_8:
  276.         need_left(1);
  277.         fidx = chr;
  278.         chr = *p++;
  279.         if_debug2('J', "[J]8/8 index=%d, char=%ld\n",
  280.               fidx, chr);
  281.         break;
  282.  
  283.         case fmap_1_7:
  284.         fidx = chr >> 7;
  285.         chr &= 0x7f;
  286.         if_debug2('J', "[J]1/7 index=%d, char=%ld\n",
  287.               fidx, chr);
  288.         break;
  289.  
  290.         case fmap_9_7:
  291.         need_left(1);
  292.         fidx = ((uint) chr << 1) + (*p >> 7);
  293.         chr = *p & 0x7f;
  294.         if_debug2('J', "[J]9/7 index=%d, char=%ld\n",
  295.               fidx, chr);
  296.         p++;
  297.         break;
  298.  
  299.         case fmap_SubsVector:
  300.         {
  301.             int width = pdata->subs_width;
  302.             uint subs_count = pdata->subs_size;
  303.             const byte *psv = pdata->SubsVector.data;
  304.  
  305. #define subs_loop(subs_elt, width)\
  306.   while ( subs_count != 0 && tchr >= (schr = subs_elt) )\
  307.     subs_count--, tchr -= schr, psv += width;\
  308.   chr = tchr; p += width - 1; break
  309.  
  310.             switch (width) {
  311.             default:    /* can't happen */
  312.                 return_error(gs_error_invalidfont);
  313.             case 1:
  314.                 {
  315.                 byte tchr = (byte) chr, schr;
  316.  
  317.                 subs_loop(*psv, 1);
  318.                 }
  319.             case 2:
  320.                 need_left(1);
  321. #define w2(p) (((ushort)*p << 8) + p[1])
  322.                 {
  323.                 ushort tchr = ((ushort) chr << 8) + *p,
  324.                        schr;
  325.  
  326.                 subs_loop(w2(psv), 2);
  327.                 }
  328.             case 3:
  329.                 need_left(2);
  330. #define w3(p) (((ulong)*p << 16) + ((uint)p[1] << 8) + p[2])
  331.                 {
  332.                 ulong tchr = ((ulong) chr << 16) + w2(p),
  333.                       schr;
  334.  
  335.                 subs_loop(w3(psv), 3);
  336.                 }
  337.             case 4:
  338.                 need_left(3);
  339. #define w4(p) (((ulong)*p << 24) + ((ulong)p[1] << 16) + ((uint)p[2] << 8) + p[3])
  340.                 {
  341.                 ulong tchr = ((ulong) chr << 24) + w3(p),
  342.                       schr;
  343.  
  344.                 subs_loop(w4(psv), 4);
  345.                 }
  346. #undef w2
  347. #undef w3
  348. #undef w4
  349. #undef subs_loop
  350.             }
  351.             fidx = pdata->subs_size - subs_count;
  352.             if_debug2('J', "[J]SubsVector index=%d, char=%ld\n",
  353.                   fidx, chr);
  354.             break;
  355.         }
  356.  
  357.         case fmap_CMap:
  358.         {
  359.             gs_const_string cstr;
  360.             uint mindex = p - str - 1;    /* p was incremented */
  361.             int code;
  362.  
  363.             cstr.data = str;
  364.             cstr.size = end - str;
  365.             code = gs_cmap_decode_next(pdata->CMap, &cstr, &mindex,
  366.                            &fidx, &chr, &glyph);
  367.             if (code < 0)
  368.             return code;
  369.             pte->cmap_code = code; /* hack for widthshow */
  370.             p = str + mindex;
  371.             if_debug3('J', "[J]CMap returns %d, chr=0x%lx, glyph=0x%lx\n",
  372.                   code, (ulong) chr, (ulong) glyph);
  373.             if (code == 0) {
  374.             if (glyph == gs_no_glyph) {
  375.                 glyph = gs_min_cid_glyph;
  376.                 if_debug0('J', "... undefined\n");
  377.                 goto done;
  378.             }
  379.             } else
  380.             chr = (gs_char) glyph, glyph = gs_no_glyph;
  381.             /****** RESCAN chr IF DESCENDANT IS CMAP'ED ******/
  382.             break;
  383.         }
  384.  
  385.     }
  386.  
  387.     select_descendant(pfont, pdata, fidx, fdepth);
  388.     if_debug2('J', "... new depth=%d, new font=0x%lx\n",
  389.           fdepth, (ulong) pfont);
  390.     /* FontBBox may be used as metrics2 with WMode=1 :
  391.     */
  392.     if (pfont->FontType == ft_CID_encrypted ||
  393.         pfont->FontType == ft_CID_TrueType
  394.         ) {
  395.         gs_font_base *pfb = (gs_font_base *)pfont;
  396.  
  397.         pte->FontBBox_as_Metrics2 = pfb->FontBBox.q;
  398.     }
  399.     }
  400. done:
  401.     *pchr = chr;
  402.     *pglyph = glyph;
  403.     /* Update the pointer into the original string, but only if */
  404.     /* we didn't switch over to parsing a code from a CMap. */
  405.     if (str == pte->text.data.bytes)
  406.     pte->index = p - str;
  407.     pte->fstack.depth = fdepth;
  408.     if_debug4('J', "[J]depth=%d font=0x%lx index=%d changed=%d\n",
  409.           fdepth, (ulong) pte->fstack.items[fdepth].font,
  410.           pte->fstack.items[fdepth].index, changed);
  411.     return changed;
  412. #undef pfont0
  413. }
  414.